<?php

namespace Home\Controller;
use Think\Controller;

class CodeController extends CommonController{

  //获取要发送的内容
	public function codeMessage($phone,$msg){
		//$verify = "贾腾飞";
		//手机号
		$mobile = $phone;
		$argv = array(
            'account' => 'ms-weier',
            'pswd' => 'MSweier0929',
            'msg' => $msg,
            'mobile' => $mobile,
            'product' => '',
            'needstatus' => 'false',
		);
		return $argv;
	}

  //发送操作
	public function msgSend($phone,$msg){
		//$phone = '17710470581,18513344641,13581544842';
		$message = $this->codeMessage($phone,$msg);
		$curl = "http://114.55.176.84/msg/HttpSendSM";
		$ch = curl_init();
		curl_setopt($ch,CURLOPT_URL,$curl);
		curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
		curl_setopt($ch,CURLOPT_POST,1);
		curl_setopt($ch,CURLOPT_POSTFIELDS,http_build_query($message));
		$output = curl_exec($ch);
		curl_close($ch);
		$arr = explode(',',$output);
		return $arr['1'];


		//http://IP:PORT/msg/HttpPkgSM
		//$params = '';
		// $flag = 0;
		// foreach($message as $key=>$value){
		// 	if ($flag !=0) { 
		// 		$params .= "&"; 
		// 		$flag = 1; 
		//     } 
		// 	$params.= $key."="; $params.= urlencode($value);
		// 	$flag = 1;
		// }
		//var_dump($params);exit;
		//$url = "http://114.55.176.84/msg/HttpSendSM?".$params; 
		//$con= file_get_contents($url);  
		//$str = substr($con,-1,1);
	    ///echo $str;exit;
	 //    　  $url = "http://114.55.176.84/msg/HttpSendSM$message";
		// 　　$post_data = $message;

		// 　　$ch = curl_init();

		// 　　curl_setopt($ch, CURLOPT_URL, $url);
		// 　　curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
		// 　　// post数据
		// 　　curl_setopt($ch, CURLOPT_POST, 1);
		// 　　// post的变量
		// 　　curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);

		// 　　$output = curl_exec($ch);
		// 　　curl_close($ch);

		// 　　//打印获得的数据
		// 　　print_r($output);

	}

    
	//微信推送消息
	public function token(){
        $result = file_get_contents('https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=wxef165ed7272bd44e&secret=3cf94e7bb2d3e02bf869552838bbead2');
        //var_dump(json_decode($result,true));exit;
        return $result;
	}
    
    //调用时传递  uid type   type2(是否调用token默认为空不调用   1为调用)   jinbi(在奖励中使用)默认为空
	//发送消息
	public function send($uid,$type,$type2='',$jinbi='',$status=''){
		$type = $this->type($uid,$type,$jinbi,$status);
		if($type['status'] == -1){ $str = '该用户没有关注微信公众号'; return $str; }
        $val = json_decode($this->token(),true);
        $access_token = $val['access_token'];
		//要发送的数据
		$data = array(
            'touser' => $type['openid'],
			'template_id' => $type['moban'],
			'data' => $type['array'],    
		);
		$url = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token={$access_token}";
		$ch = curl_init();
		curl_setopt($ch,CURLOPT_URL,$url);
		curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
		curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,0);
		curl_setopt($ch,CURLOPT_POST,1);
		curl_setopt($ch,CURLOPT_POSTFIELDS,json_encode($data));
		$vall = curl_exec($ch);
		curl_close($ch);
		var_dump($vall);exit;

	}


	//判断要发送的类型
	/**
	* @  1   用户信用等级审核结果通知
	* @  2   用户借款申请受理通知
	* @  3   用户借款审核结果通知
	* @  4   用户借款申请取消通知
	* @  5   用户还款提前通知
	* @  6   用户还款失败及成果通知
	* @  7   用户获得奖励通知
	*/
	public function type($uid,$type,$jinbi){
        $openid = M('cash_user_wx')->where("wx_uid = {$uid}")->field('nickname,openid')->find();
        if(!$openid){
        	$result['static'] = -1;
        	return $result;
        }
		if($type == 7){
			$result['moban'] = 'O6HtqVDOepsj_t-IZh1rEC79b_dHr4q1i5p4rQ-YD7c';
            $userGold = M('cash_user_gold')->where("uid = {$uid}")->field('number')->find();
            $result['array'] = array(
                        'first' => array('value' => '您的账户金币变更如下'),
                        ['change' => '增加'].'积分' => array('CreditChange' => $jinbi),
                        'CreditTotal' => $userGold['number'],
                        'Remark' => '您可在微信公众号和app中体现金币',
                    );
            $result['openid'] = $openid['openid'];
            return $result;  
		}           
		if($type == 5){
			$result['moban'] = 'lTO1cwMG7vGOHnXiQH222GIF0F03pH1Bfipwyy1HTUM';
			$val = M('cash_order')->where("uid = {$uid} AND status = 4")->order('creat_time desc')->find();
			$result['array'] = array(
                        'first' => array('value' => '您好,你在微耳钱包的借款即将到期,请尽快还款'),
                        'dpbh' => array('value' => $val['order_id']),
                        'ddrq' => array('value' => $val['loan_time']),
                        'ddje' => array('value' => $val['money']),
                        'ddlastrq' => array('value' => $val['expire_time']),
                        'remark' => array('value' => '请尽快还款,以免给您生活带来影响'),
			        );
            $result['openid'] = $openid['openid'];
            return $result;
		}

		if(in_array($type,[2,4])){
			$result['moban'] = 'n2WEaj3_t9KO-jShVZyHPWzhr42p9j9JV_ZWzGyWEyY';
			$time = M('cash_order')->where("uid = {$uid}")->order('creat_time desc')->field('order_id,creat_time')->find();
			if($status == 1){ $status = '通过';}else{ $status = '拒绝';}
			$result['array'] = array(
                    'first' => array('value' => '微耳钱包来电'),
                    'keyword1' => array('value' => $time['order_id']),
                    'keyword2' => array('value' => 0),
                    'keyword3' => array('value' => date('Y-m-d H:i:s',$time['creat_time'])),
                    'keyword4' => array('value' => $status),
                    'remark' => array('value' => '如有疑问请联系微耳客服'),
			    );
			$result['openid'] = $openid['openid'];
			return $result;
 		}
		if($type == 3){
			$moban = 'S0Mo_2TPluMfNxk5h9Rv6ifqDjv2OwCzje2IbhcADC0';
			//编写要发送的数据

			//获取当前用户的姓名  借款金额  借款期限  利率
		}

		if($type == 1){
			$result['moban'] = 'UhB6_X7Ff_qJ8s8gMTY7WYvKkvwU_MIzaOstfBkpxsg';
			$result['array'] = array(
                        'first' => array('value' => '您在我们平台上的等级评分'),
                        'keyword1' => array('value' => $openid['nickname']),
                        'keyword2' => array('value' => '555'),
                        'keyword3' => array('value' => 'H'),
                        'keyword4' => array('value' => date('Y-m-d H:i:s',time())),
                        'remark' => '继续保持等级',
			        );
			$result['openid'] = $openid['openid'];
			return $result;
		}
	}
	
	
	//获取当天到期的订单
	public function order(){
		 $Orders = M('cash_order')->where('collection_id = 0 AND status = 4')->field('order_id,expire_time,uid')->select();
		 foreach($Orders as $k=>$v){
			 $overday = overdue($v['expire_time']);
			 if($overday == 0){
				 $arr[] = $v;
				 $phone[] = M('cash_user_bank')->where("uid = {$v['uid']}")->field('yu_tel')->find()['yu_tel'];
				 //$tel[] = $phone['yu_tel'];
			 }
		 }
		 //$str = implode(';',$phone);
		 $str = '17710470581';
		 var_dump($str);
		 return $str;
	}
	
	
	
	//小灵呼操作
	public function creat(){
		$message = array(
			'vo_id'         => 'bctask_create',
			'bcall_memo'    => 'renwul1',
			'dt_create'     => '2017-11-01',
			'type_transfer' => '1',
			'flow_calloutb' => '_F_PLAY_TRANS2AGENT_',
			'type_try'      => 0,
			'cnt_max_try'   => 3,
			'interval_try'  => 60,
			'time_type'     => 1,
			'workTime'      => '00:00:00,23:59:59;00:00:00,00:00:00;00:00:00,23:59:59;00:00:00,00:00:00;00:00:00,23:59:59;00:00:00,00:00:00;00:00:00,23:59:59;00:00:00,00:00:00;00:00:00,23:59:59;00:00:00,00:00:00;',
			'repType'       => 'xml',
			'vtype'         => 'B',
			'skill'         => 'default',
			'rateOut'       => '2.0',
			'rateAgent'     => 1,
			//'bcall_batch'   => '自动',
			'id_oper'       => 1000,
			'code_area'     => 10010101,
			'code_area2'    => 10010100,
			'code_area3'    => 10010000,
			'_secs_'        => 'lcall'
		);
		$output = $this->curl($message);
		var_dump($output);exit; 
		$obj = simplexml_load_string($output,'SimpleXMLElement',LIBXML_NOCDATA);
		$val = json_decode(json_encode($obj),true);
        return $val;
	}
	
	//添加手机号操作
	public function add(){
		$obj = $this->creat();
		$phone = $this->order();
		$message = array(
			'vo_id'          => 'ctask_add',
			'bcall_batch'    => $obj['bctask']['bcall_batch'],
			'ifNoDup'        => 'false',
			'phone_calloutb' => $phone,
			'repType'        => 'xml',
			'vtype'          => 'B',
			'_secs_'         => 'lcall'
		);
		$output = $this->curl($message);
		$obj = simplexml_load_string($output,'SimpleXMLElement',LIBXML_NOCDATA);
		$val = json_decode(json_encode($obj),true);	
		return $val;
	}
	
	//任务发送操作
	public function start(){
		$add = $this->add();
		$message = array(
			'vo_id'       => 'bctask_start',
			'bcall_batch' => $add['bctask']['bcall_batch'],
			'ifNoDup'     => 'false',
			'repType'     => 'xml',
			'vtype'       => 'B',
			'_secs_'      => 'lcall'
		);
		$output = $this->curl($message);
		var_dump($output);exit;
	}
	
	//curl操作、
	public function curl($message){
		var_dump($message);
		$curl = "http://192.168.1.235:7627/v/vack";
		$ch = curl_init();
		curl_setopt($ch,CURLOPT_URL,$curl);
		curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
		curl_setopt($ch,CURLOPT_POST,1);
		curl_setopt($ch,CURLOPT_POSTFIELDS,http_build_query($message));
		$output = curl_exec($ch);
		curl_close($ch);
		return $output;
	}

}